<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_warehouse.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_goods_attr.php');
require(dirname(__FILE__) . '/includes/lib/cls/cls_date.php');
require(dirname(__FILE__) . '/includes/lib/lib_erp_goods.php');

/*------------------------------------------------------ */
//-- 设置仓库
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'warehouse_list')
{
	
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$warehouse_info=get_warehouse_info();
		
		$action_link = array('href' => 'erp_warehouse_manage.php?act=add_warehouse', 'text' => $_LANG['erp_add_warehouse']);
    $smarty->assign('action_link',  $action_link);
    
    $smarty->assign('admins',  get_admin());

		$smarty->assign('warehouse_info',  $warehouse_info);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_warehouse_list']);
		$smarty->display('erp_warehouse_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'add_warehouse')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$smarty->assign('admins',  get_admin());
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_add_warehouse']);
		$smarty->display('erp_warehouse_info.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'warehousing_style')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$warehousing_style=get_warehousing_style_info();

		$action_link = array('href' => 'erp_warehouse_manage.php?act=add_warehousing_style', 'text' => $_LANG['erp_add_warehousing_style']);
    $smarty->assign('action_link',  $action_link);

		$smarty->assign('warehousing_style',  $warehousing_style);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_warehousing_style']);
		$smarty->display('erp_warehousing_style.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'add_warehousing_style')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_add_warehousing_style']);
		$smarty->display('erp_warehousing_style_info.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'delivery_style')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$delivery_style=get_delivery_style_info();

		$action_link = array('href' => 'erp_warehouse_manage.php?act=add_delivery_style', 'text' => $_LANG['erp_add_delivery_style']);
    $smarty->assign('action_link',  $action_link);

		$smarty->assign('delivery_style',  $delivery_style);
		
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_delivery_style']);
		$smarty->display('erp_delivery_style.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'add_delivery_style')
{
	if((admin_priv('erp_sys_manage', '', false)))
	{	
		$smarty->assign('ur_here', $GLOBALS['_LANG']['erp_add_delivery_style']);
		$smarty->display('erp_delivery_style_info.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'warehousing_list')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		include('./includes/lib/page.class.php');
		
		//收货仓库信息
		$warehouse_info=get_warehouse_info();
		$smarty->assign('warehouse_info',  $warehouse_info);
		
		//入库类型
		$warehousing_style=get_warehousing_style_info();
		$smarty->assign('warehousing_style',  $warehousing_style);
		
		//入库单状态
		$warehousing_status=get_warehousing_status();
		$smarty->assign('warehousing_status',  $warehousing_status);
		
		//分页类参数
		$num_per_page=20;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		if(!empty($_REQUEST['warehousing_sn']))
		{
			$warehousing_info=get_warehousing_info('',$_REQUEST['warehousing_sn']);
			$total_num=count($warehousing_info);
		}
		else{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
			$start=$num_per_page*($page-1);
			
			//echo $start;
			
			$warehouse_id=isset($_REQUEST['w_id'])?($_REQUEST['w_id']):0;
			$warehousing_style_id=isset($_REQUEST['w_s_id'])?($_REQUEST['w_s_id']):0;
			
			$warehousing_status=isset($_REQUEST['w_s'])?($_REQUEST['w_s']):0;
			
			/*
			if((admin_priv('erp_warehouse_manage', '', false)) && !(admin_priv('erp_warehouse_rate', '', false)) && !(admin_priv('erp_warehouse_approve', '', false)))
			{
				$warehousing_status=1;
			}	
			elseif(!(admin_priv('erp_warehouse_manage', '', false)) && (admin_priv('erp_warehouse_rate', '', false)) && !(admin_priv('erp_warehouse_approve', '', false)))
			{
				$warehousing_status=2;
			}					
			elseif(!(admin_priv('erp_warehouse_manage', '', false)) && !(admin_priv('erp_warehouse_rate', '', false)) && (admin_priv('erp_warehouse_approve', '', false)))
			{
				$warehousing_status=3;
			}
			else{
				$warehousing_status=0;
			}
			*/
			
			$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
			$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';
			$warehousing_from=isset($_REQUEST['w_f'])?($_REQUEST['w_f']):'';
			
			$cls_date=new cls_date();
			
			if(!empty($start_date))
			{
				$start_time=$cls_date->date_to_stamp($start_date);
			}
			if(!empty($end_date))
			{
				$end_time=$cls_date->date_to_stamp($end_date);
			}			
			
			$total_num=count(get_warehousing_info('','',$warehousing_status,$start_time,$end_time,$warehousing_style_id,$warehouse_id,$warehousing_from));
		
			$warehousing_info=get_warehousing_info('','',$warehousing_status,$start_time,$end_time,$warehousing_style_id,$warehouse_id,$warehousing_from,'',$start,$num_per_page);
				
			$url="erp_warehouse_manage.php?act=warehousing_list&w_id=".$warehouse_id."&w_s_id=".$warehousing_style_id."&w_s=".$warehousing_status."&s_date=".$start_date."&e_date=".$end_date."&w_f=".$warehousing_from;	
		}
		
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());	
		
		if((admin_priv('erp_warehouse_manage', '', false)))
		{
			//$action_link = array('href' => 'erp_warehouse_manage.php?act=warehousing', 'text' => $_LANG['erp_goods_warehousing']);
			$action_link = array('href' => 'javascript: add_warehousing();', 'text' => $_LANG['erp_goods_warehousing']);
			$smarty->assign('action_link',  $action_link);
		}		
		
		$smarty->assign('warehousing_info',  $warehousing_info);
		
		$smarty->assign('ur_here',  $_LANG['erp_goods_warehousing_list']);
		$smarty->display('erp_warehousing_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'view_warehousing')
{	
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		$warehousing_id=intval($_REQUEST[id]);
		
		$warehousing_info=get_warehousing_info($warehousing_id);
		$warehousing_item_info=get_warehousing_item_info($warehousing_id);

		$smarty->assign('warehousing_info',  $warehousing_info[0]);
		$smarty->assign('warehousing_item_info',  $warehousing_item_info);
		
		$smarty->assign('act',  'view');

		//只有主管才可以查看价格
		if((admin_priv('erp_warehouse_approve', '', false)))
		{
			$smarty->assign('price_visible',  'true');
		}

		$smarty->assign('ur_here',  $_LANG['erp_warehousing_view_warehousing']);
		$smarty->display('erp_warehousing_info.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'print_warehousing')
{	
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		$warehousing_id=intval($_REQUEST[id]);
		
		$warehousing_info=get_warehousing_info($warehousing_id);
		$warehousing_item_info=get_warehousing_item_info($warehousing_id);		
		
		$smarty->assign('warehousing_info',  $warehousing_info[0]);
		$smarty->assign('warehousing_item_info',  $warehousing_item_info);
		
		$smarty->assign('act',  'view');
		
		$smarty->display('erp_warehousing_print.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'edit_warehousing')
{
	if((admin_priv('erp_warehouse_manage', '', false)))
	{	
		$warehousing_id=intval($_REQUEST[id]);
		if(lock_table($warehousing_id,'warehousing','edit'))//锁定订单
		{		
			//获取入库单信息
			$warehousing_info=get_warehousing_info($warehousing_id);
			//获取入库单项信息
			$warehousing_item_info=get_warehousing_item_info($warehousing_id);
			//获取仓库信息
			$warehouse_info=get_warehouse_info('',erp_get_admin_id(),1);
			//获取入库类型
			$warehousing_style_info=get_warehousing_style_info('',1);
		
			//如果入库类型是关联订单入库，获取所有未入库完的订单信息
			if($warehousing_info[0]['warehousing_style_id']==1)
			{
				$order_info=get_order_not_warehoused();
				$smarty->assign('order_info',  $order_info);
			}

			$smarty->assign('warehousing_info',  $warehousing_info[0]);
			$smarty->assign('warehousing_item_info',  $warehousing_item_info);
			$smarty->assign('warehouse_info',  $warehouse_info);
			$smarty->assign('warehousing_style_info',  $warehousing_style_info);
		
			$smarty->assign('act',  'edit');
			
			//只有主管才可以查看价格
			if(admin_priv('erp_warehouse_approve', '', false))
			{
				$smarty->assign('price_visible',  'true');
			}
			
			$smarty->assign('ur_here',  $_LANG['erp_warehousing_edit_warehousing']);
			$smarty->display('erp_warehousing_info.htm');
		}
		else{
			$href="./erp_warehouse_manage.php?act=warehousing_list";
			$text=$_LANG['erp_warehousing_return_to_warehousing_list'];
			$link[] = array('href' => $href, 'text' => $text);	
			
			$href="./erp_warehouse_manage.php?act=view_warehousing&id=".$warehousing_id;
			$text=$_LANG['erp_warehousing_view_warehousing'];
			$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_warehousing_no_accessibility'], 0, $link);
		}
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'approve_warehousing')
{
	if((admin_priv('erp_warehouse_approve', '', false)))
	{	
		$warehousing_id=intval($_REQUEST[id]);
		if(lock_table($warehousing_id,'warehousing','approve'))//锁定订单
		{		
			//获取入库单信息
			$warehousing_info=get_warehousing_info($warehousing_id);
			//获取入库单项信息
			$warehousing_item_info=get_warehousing_item_info($warehousing_id);
			
			//echo "<pre>";
				//print_r($warehousing_info);
			//echo "</pre>";				
		
			$smarty->assign('warehousing_info',  $warehousing_info[0]);
			$smarty->assign('warehousing_item_info',  $warehousing_item_info);
		
			$smarty->assign('act',  'approve');

			//只有主管才可以查看价格
			if((admin_priv('erp_warehouse_approve', '', false)))
			{
				$smarty->assign('price_visible',  'true');
			}
			
			$smarty->assign('ur_here',  $_LANG['erp_warehousing_approve_warehousing']);
			$smarty->display('erp_warehousing_info.htm');
		}
		else{
			$href="./erp_warehouse_manage.php?act=warehousing_list";
			$text=$_LANG['erp_warehousing_return_to_warehousing_list'];
			$link[] = array('href' => $href, 'text' => $text);	
			
			$href="./erp_warehouse_manage.php?act=view_warehousing&id=".$warehousing_id;
			$text=$_LANG['erp_warehousing_view_warehousing'];
			$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_warehousing_no_accessibility'], 0, $link);
		}
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'goods_list')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		include('./includes/lib/page.class.php');

		//分页类参数
		$num_per_page=20;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
	
		if(!empty($_REQUEST['goods_sn']))
		{
			$goods_info=get_goods_info('',$_REQUEST['goods_sn']);
			$total_num=count($goods_info);
		}
		else
		{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
			$sort=isset($_REQUEST['sort'])?($_REQUEST['sort']):'last_update';
			$sort_type=isset($_REQUEST['sort_type'])?($_REQUEST['sort_type']):'asc';	
		
			$start=$num_per_page*($page-1);
		
			if(isset($_REQUEST['cat_id']))
			{
			
				$cat_id=intval($_REQUEST['cat_id']);
			
				$total_num=count(get_goods_info('','',-1,-1,$cat_id));
				
				$goods_info=get_goods_info('','',$start,$num_per_page,$cat_id,$sort,$sort_type);
			
				$url="erp_warehouse_manage.php?act=goods_list&cat_id=".$cat_id."&sort=".$sort."&sort_type=".$sort_type;
			}
			else{
				$total_num=count(get_goods_info());
				$goods_info=get_goods_info('','',$start,$num_per_page,'',$sort,$sort_type);	
				$url="erp_warehouse_manage.php?act=goods_list&sort=".$sort."&sort_type=".$sort_type;		
			}		
		}
	
		//初始化分页类
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
			
		$smarty->assign('page',  $page->show());
		$smarty->assign('goods_info',  $goods_info);
		
		
		$cat_info=get_cat_info('');
		$smarty->assign('cat_info',  $cat_info);

		if(admin_priv('erp_warehouse_manage', '', false))
		{
			$smarty->assign('edit',  1);
		}
		
		$smarty->assign('url',  $_SERVER["REQUEST_URI"]);
		
		$smarty->assign('ur_here',  $_LANG['erp_goods_list']);
		$smarty->display('erp_goods_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'delivery_list')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		include('./includes/lib/page.class.php');
		
		//发货仓库信息
		$warehouse_info=get_warehouse_info();
		$smarty->assign('warehouse_info',  $warehouse_info);
		
		//出库类型
		$delivery_style=get_delivery_style_info();
		$smarty->assign('delivery_style',  $delivery_style);

		//出库单状态
		$delivery_status=get_delivery_status();
		$smarty->assign('delivery_status',  $delivery_status);
		
		//分页类参数
		$num_per_page=20;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		if(!empty($_REQUEST['delivery_sn']))
		{
			$delivery_info=get_delivery_info('',$_REQUEST['delivery_sn']);
			$total_num=count($delivery_info);
		}
		else{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
			$start=$num_per_page*($page-1);
			
			$warehouse_id=isset($_REQUEST['w_id'])?($_REQUEST['w_id']):0;
			$delivery_style_id=isset($_REQUEST['d_s_id'])?($_REQUEST['d_s_id']):0;
			
			$delivery_status=isset($_REQUEST['d_s'])?($_REQUEST['d_s']):0;
			
			/*
			if((admin_priv('erp_warehouse_manage', '', false)) && !(admin_priv('erp_warehouse_rate', '', false)) && !(admin_priv('erp_warehouse_approve', '', false)))
			{
				$delivery_status=1;
			}	
			elseif(!(admin_priv('erp_warehouse_manage', '', false)) && (admin_priv('erp_warehouse_rate', '', false)) && !(admin_priv('erp_warehouse_approve', '', false)))
			{
				$delivery_status=2;
			}					
			elseif(!(admin_priv('erp_warehouse_manage', '', false)) && !(admin_priv('erp_warehouse_rate', '', false)) && (admin_priv('erp_warehouse_approve', '', false)))
			{
				$delivery_status=3;
			}
			else{
				$delivery_status=0;
			}			
			*/
			
			
			$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
			$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';
			$delivery_to=isset($_REQUEST['d_t'])?($_REQUEST['d_t']):'';
			
			$cls_date=new cls_date();
			
			if(!empty($start_date))
			{
				$start_time=$cls_date->date_to_stamp($start_date);
			}
			if(!empty($end_date))
			{
				$end_time=$cls_date->date_to_stamp($end_date);
			}			
			
			$total_num=count(get_delivery_info('','',$delivery_status,$start_time,$end_time,$delivery_style_id,$warehouse_id,$delivery_to));
		
			$delivery_info=get_delivery_info('','',$delivery_status,$start_time,$end_time,$delivery_style_id,$warehouse_id,$delivery_to,$start,$num_per_page);
				
			$url="erp_warehouse_manage.php?act=delivery_list&w_id=".$warehouse_id."&d_s_id=".$delivery_style_id."&d_s=".$delivery_status."&s_date=".$start_date."&e_date=".$end_date."&d_t=".$delivery_to;	
		}
		
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
		$smarty->assign('page',  $page->show());
		
		if((admin_priv('erp_warehouse_manage', '', false)))
		{
			$action_link = array('href' => 'javascript: add_delivery()', 'text' => $_LANG['erp_goods_delivery']);
			$smarty->assign('action_link',  $action_link);
		}	
		
		$smarty->assign('delivery_info',  $delivery_info);
		
		$smarty->assign('ur_here',  $_LANG['erp_goods_delivery_list']);
		$smarty->display('erp_delivery_list.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'view_delivery')
{	
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		$delivery_id=intval($_REQUEST[id]);
		
		$delivery_info=get_delivery_info($delivery_id);
		$delivery_item_info=get_delivery_item_info($delivery_id);	
		
		$smarty->assign('delivery_info',  $delivery_info[0]);
		$smarty->assign('delivery_item_info',  $delivery_item_info);
		
		$smarty->assign('act',  'view');

		//只有主管才可以查看价格
		if((admin_priv('erp_warehouse_approve', '', false)))
		{
			$smarty->assign('price_visible',  'true');
		}
		
		$smarty->display('erp_delivery_info.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'print_delivery')
{	
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		$delivery_id=intval($_REQUEST[id]);
		
		$delivery_info=get_delivery_info($delivery_id);
		$delivery_item_info=get_delivery_item_info($delivery_id);	
		
		$smarty->assign('delivery_info',  $delivery_info[0]);
		$smarty->assign('delivery_item_info',  $delivery_item_info);
		
		$smarty->assign('act',  'view');

		//只有主管才可以查看价格
		if((admin_priv('erp_warehouse_approve', '', false)))
		{
			$smarty->assign('price_visible',  'true');
		}
		
		$smarty->display('erp_delivery_print.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'edit_delivery')
{
	
	if((admin_priv('erp_warehouse_manage', '', false)))
	{	
		$delivery_id=intval($_REQUEST[id]);
		if(lock_table($delivery_id,'delivery','edit'))//锁定订单
		{		
			//获取入库单信息
			$delivery_info=get_delivery_info($delivery_id);
			//获取入库单项信息
			$delivery_item_info=get_delivery_item_info($delivery_id);
			//获取仓库信息
			$warehouse_info=get_warehouse_info('',erp_get_admin_id(),1);
			//获取入库类型
			$delivery_style_info=get_delivery_style_info('',1);
		
			//如果入库类型是关联订单入库，获取所有未入库完的订单信息
			if($delivery_info[0]['delivery_style_id']==1)
			{
				$order_info=get_order_not_delivered();
				$smarty->assign('order_info',  $order_info);
			}		
		
			$smarty->assign('delivery_info',  $delivery_info[0]);
			$smarty->assign('delivery_item_info',  $delivery_item_info);
			$smarty->assign('warehouse_info',  $warehouse_info);
			$smarty->assign('delivery_style_info',  $delivery_style_info);
		
			$smarty->assign('act',  'edit');

			//只有主管才可以查看价格
			if((admin_priv('erp_warehouse_approve', '', false)))
			{
				$smarty->assign('price_visible',  'true');
			}
		
			$smarty->display('erp_delivery_info.htm');
		}
		else{
			$href="./erp_warehouse_manage.php?act=delivery_list";
			$text=$_LANG['erp_delivery_return_to_delivery_list'];
			$link[] = array('href' => $href, 'text' => $text);	
			
			$href="./erp_warehouse_manage.php?act=view_delivery&id=".$delivery_id;
			$text=$_LANG['erp_delivery_view_delivery'];
			$link[] = array('href' => $href, 'text' => $text);

   		sys_msg($_LANG['erp_delivery_no_accessibility'], 0, $link);
		}
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'approve_delivery')
{
	if((admin_priv('erp_warehouse_approve', '', false)))
	{	
		$delivery_id=intval($_REQUEST[id]);
		if(lock_table($delivery_id,'delivery','approve'))//锁定订单
		{		
			//获取入库单信息
			$delivery_info=get_delivery_info($delivery_id);
			//获取入库单项信息
			$delivery_item_info=get_delivery_item_info($delivery_id);	
		
			$smarty->assign('delivery_info',  $delivery_info[0]);
			$smarty->assign('delivery_item_info',  $delivery_item_info);
		
			$smarty->assign('act',  'approve');

			//只有主管才可以查看价格
			if((admin_priv('erp_warehouse_approve', '', false)))
			{
				$smarty->assign('price_visible',  'true');
			}
		
			$smarty->display('erp_delivery_info.htm');
		}
		else{
			$href="./erp_warehouse_manage.php?act=delivery_list";
			$text=$_LANG['erp_delivery_return_to_delivery_list'];
			$link[] = array('href' => $href, 'text' => $text);	
			
			$href="./erp_warehouse_manage.php?act=view_delivery&id=".$delivery_id;
			$text=$_LANG['erp_delivery_view_delivery'];
			$link[] = array('href' => $href, 'text' => $text);

   			sys_msg($_LANG['erp_delivery_no_accessibility'], 0, $link);
		}
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'stock_inquiry')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		
		$warehouse_info=get_warehouse_info();

		$smarty->assign('warehouse_info',  $warehouse_info);
		
		include('./includes/lib/page.class.php');

		//分页类参数
		$num_per_page=20;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		//如果搜索产品货号
		if(!empty($_REQUEST['goods_sn']))
		{
			$goods_sn=$_REQUEST['goods_sn'];
			$sql="select goods_id from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
			$goods_id=$GLOBALS['db']->getOne($sql);
			
			$warehouse_id=$_REQUEST['warehouse_id'];
			
			$stock_info=get_erp_stock($warehouse_id,$goods_id);
			$total_num=count($stock_info);
		}
		//如果搜索产品供应商货号
		elseif(!empty($_REQUEST['provider_goods_sn']))
		{
			$provider_goods_sn=$_REQUEST['provider_goods_sn'];
			$sql="select goods_id from ".$GLOBALS['ecs']->table('goods')." where provider_goods_sn='".$provider_goods_sn."'";
			$goods_id=$GLOBALS['db']->getOne($sql);
			
			$warehouse_id=$_REQUEST['warehouse_id'];
			
			$stock_info=get_erp_stock($warehouse_id,$goods_id);
			$total_num=count($stock_info);			
		}
		else
		{
			$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
		
			$start=$num_per_page*($page-1);
		
			if(isset($_REQUEST['warehouse_id']))
			{
			
				$warehouse_id=intval($_REQUEST['warehouse_id']);
			
				$total_num=count(get_erp_stock($warehouse_id,''));
				
				$stock_info=get_erp_stock($warehouse_id,'',$start,$num_per_page);
			
				$url="erp_warehouse_manage.php?act=stock_inquiry&warehouse_id=".$warehouse_id;
			}
			else{
				$total_num=count(get_erp_stock());
				$stock_info=get_erp_stock('','',$start,$num_per_page);	
				$url="erp_warehouse_manage.php?act=stock_inquiry";	
			}		
		}
	
		//初始化分页类
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));
			
		$smarty->assign('page',  $page->show());
		$smarty->assign('stock_info',  $stock_info);

		$smarty->assign('ur_here',  $_LANG['erp_stock_inquiry']);
		$smarty->display('erp_stock.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}

elseif ($_REQUEST['act'] == 'stock_check')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	

		include('./includes/lib/page.class.php');

		//分页类参数
		$num_per_page=50;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类

		$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
		$start=$num_per_page*($page-1);		

		$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
		$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';
		
		$warehouse_id=isset($_REQUEST['warehouse_id'])?($_REQUEST['warehouse_id']):0;
		
		$cls_date=new cls_date();
		
		if(!empty($start_date))
		{
			$start_time=$cls_date->date_to_stamp($start_date);
		}
		if(!empty($end_date))
		{
			$end_time=$cls_date->date_to_stamp($end_date);
		}		
		
		$warehouse_info=get_warehouse_info('',erp_get_admin_id(),1);

		//生成库存盘点表
		update_tb_stock_check($start_time,$end_time,$warehouse_id);
		
		$total_num=count(get_stock_check_info());
		$stock_check_info=get_stock_check_info($start,$num_per_page);
		
		$url="erp_warehouse_manage.php?act=stock_check&s_date=".$start_date."&e_date=".$end_date;	
		
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));			

		$smarty->assign('start_date',  $start_date);
		$smarty->assign('end_date',  $end_date);
		$smarty->assign('warehouse_id',  $warehouse_id);
		
		$smarty->assign('page',  $page->show());
		$smarty->assign('stock_check_info',  $stock_check_info);
		$smarty->assign('warehouse_info',  $warehouse_info);
		
		$smarty->assign('ur_here',  $_LANG['erp_stock_check']);
		$smarty->display('erp_stock_check.htm');			
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'print_stock_check')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		$start_date=$_REQUEST['start_date'];
		$end_date=$_REQUEST['end_date'];
		
		$cls_date=new cls_date();
		
		$current_date=date('Y-m-d',time());
		$first_date=$cls_date->get_first_day($current_date);
		$last_date=$cls_date->get_last_day($current_date);	
		
		//如果期末日期为空,默认为当月最后一天
		if(empty($end_date))
		{
			$end_date=$last_date;
		}
		//如果期初日期为空,默认为当月的1号
		if(empty($start_date))
		{
			$start_date=$first_date;
		}	

		$stock_check_info=get_stock_check_info();
	
		$smarty->assign('start_date',  $start_date);
		$smarty->assign('end_date',  $end_date);
		$smarty->assign('stock_check_info',  $stock_check_info);
		
		$smarty->display('erp_stock_check_print.htm');		
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}



elseif ($_REQUEST['act'] == 'export_stock_check')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		require('./includes/lib/cls/cls_csv.php');	
		require('./includes/lib/cls/cls_file.php');	
		include('../includes/cls_json.php');
			
		$stock_check_info=get_stock_check_info();

		$export=array();
		//csv首行
		$row['goods_sn']=$_LANG['erp_stock_check_goods_sn'];
		$row['warehouse_name']=$_LANG['erp_stock_check_warehouse_name'];
		$row['goods_attr']=$_LANG['erp_stock_check_goods_attr'];
		$row['begin_qty']=$_LANG['erp_stock_check_begin_qty'];
		$row['warehousing_qty']=$_LANG['erp_stock_check_warehousing_qty'];
		$row['delivery_qty']=$_LANG['erp_stock_check_delivery_qty'];
		$row['end_qty']=$_LANG['erp_stock_check_end_qty'];
		$export[]=$row;		
	

		if(!empty($stock_check_info))
		{
			//csv数据
			foreach($stock_check_info as $k=>$v)
			{
				$row['goods_sn']=$v['goods_info']['goods_sn'];
				$row['warehouse_name']=$v['warehouse_info']['name'];
				
				$goods_attr_id=$v['goods_attr_id'];
				
				if(!empty($goods_attr_id))
				{
					$goods_attr=get_attr_info($goods_attr_id);
					
					$attr='';
					
					foreach($goods_attr['attr_info'] as $item)
					{
						$attr.=$item['attr_value'];
					}
					
					$row['goods_attr']=$attr;				
				}
				else{
					$row['goods_attr']='$$';	
				}
			
				$row['begin_qty']=$v['begin_qty'];
				$row['warehousing_qty']=$v['warehousing_qty'];
				$row['delivery_qty']=$v['delivery_qty'];
				$row['end_qty']=$v['end_qty'];
				
				$export[]=$row;
			}
		}	
		
		$csv=new cls_csv();
		$file_stream=$csv->encode($export);
		
		$file_stream=iconv('utf-8','gb2312',$file_stream);
		
		$file=new cls_file();
		
		$path=ROOT_PATH.'data/stock_check/';
		$random_name=random_string('numeric', 8);
		$file_name=date('Y-m-d').'-'.$random_name.'.csv';
		
		while(file_exists($path.$file_name))//如果文件已存在那么重新生成名字
		{
			$random_name=random_string('numeric', 8);
			$file_name=date('Y-m-d').'-'.$random_name.'.csv';			
		}
		
		$file->write_file($path,$file_name,$file_stream,$op='rewrite');	//生成文件
		
		$json  = new JSON;
		
		$server_address=$_SERVER["SERVER_NAME"];
		$phpself=$_SERVER["PHP_SELF"];
		$path=str_replace(ADMIN_PATH.'/erp_warehouse_manage.php','',$phpself);
		$base_url=$server_address.$path;	
		$url='http://'.$base_url.'data/stock_check/'.$file_name;
	
		$result['error']=0;
		$result['url']=$url;
		
		die($json->encode($result));
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   		sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}	
}


elseif ($_REQUEST['act'] == 'W_D_record')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		$warehouse_info=get_warehouse_info();
		$smarty->assign('warehouse_info',  $warehouse_info);
		
		include('./includes/lib/page.class.php');

		//分页类参数
		$num_per_page=20;
		$mode=1;
		$page_bar_num=6;
		$page_style="page_style";//页码css类
		$current_page_style="current_page_style";//当前页css类
		
		$page=isset($_REQUEST['page'])?($_REQUEST['page']):1;
		$start=$num_per_page*($page-1);		
		
		$goods_sn=isset($_REQUEST['goods_sn'])?($_REQUEST['goods_sn']):'';
		$stock_style=isset($_REQUEST['s_s'])?($_REQUEST['s_s']):0;
		$warehousing_delivery_style_id=isset($_REQUEST['w_d_s_id'])?($_REQUEST['w_d_s_id']):'';
		$warehouse_id=isset($_REQUEST['w_id'])?($_REQUEST['w_id']):0;
		
		$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
		$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';
		
		$smarty->assign('goods_sn',  $goods_sn);		
		$smarty->assign('stock_style',  $stock_style);		
		$smarty->assign('warehousing_delivery_style_id',  $warehousing_delivery_style_id);		
		$smarty->assign('warehouse_id',  $warehouse_id);		
		$smarty->assign('start_date',  $start_date);		
		$smarty->assign('end_date',  $end_date);		
		
		$cls_date=new cls_date();
		
		if(!empty($start_date))
		{
			$start_time=$cls_date->date_to_stamp($start_date);
		}
		else{
			$start_time='';
		}
		
		if(!empty($end_date))
		{
			$end_time=$cls_date->date_to_stamp($end_date);
		}
		else{
			$end_time='';
		}		
		
		if(!empty($goods_sn))
		{
			$sql="select goods_id from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
			$goods_id=$GLOBALS['db']->getOne($sql);			
		}
		
		$total_num=count(get_stock_record('',$stock_style,$goods_id,$warehouse_id,$warehousing_delivery_style_id,$start_time,$end_time));
		
		$stock_record=get_stock_record('',$stock_style,$goods_id,$warehouse_id,$warehousing_delivery_style_id,$start_time,$end_time,$start,$num_per_page);
		
		$url="erp_warehouse_manage.php?act=W_D_record&goods_sn=".$goods_sn."&s_s=".$stock_style."&w_d_s_id=".$warehousing_delivery_style_id."&w_id=".$warehouse_id."&s_date=".$start_date."&e_date=".$end_date;	
		
		$page=new page(array('total_data'=>$total_num,'data_per_page'=>$num_per_page,'url'=>$url,'mode'=>$mode,'page_bar_num'=>$page_bar_num,'page_style'=>$page_style,'current_page_style'=>$current_page_style));		
		

		$smarty->assign('page',  $page->show());
		$smarty->assign('stock_record',  $stock_record);
		
		$smarty->assign('ur_here',  $_LANG['erp_goods_warehousing_and_delivery_record']);
		
		$smarty->display('erp_stock_record.htm');
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		
}


elseif ($_REQUEST['act'] == 'print_W_D_record' || $_REQUEST['act'] == 'export_W_D_record')
{
	if(admin_priv('erp_warehouse_view', '', false) || admin_priv('erp_warehouse_manage', '', false) || admin_priv('erp_warehouse_approve', '', false))
	{	
		$warehouse_info=get_warehouse_info();
		$smarty->assign('warehouse_info',  $warehouse_info);
		
		$goods_sn=isset($_REQUEST['goods_sn'])?($_REQUEST['goods_sn']):'';
		$stock_style=isset($_REQUEST['s_s'])?($_REQUEST['s_s']):0;
		$warehousing_delivery_style_id=isset($_REQUEST['w_d_s_id'])?($_REQUEST['w_d_s_id']):'';
		$warehouse_id=isset($_REQUEST['w_id'])?($_REQUEST['w_id']):0;
		
		$start_date=isset($_REQUEST['s_date'])?($_REQUEST['s_date']):'';
		$end_date=isset($_REQUEST['e_date'])?($_REQUEST['e_date']):'';
		
		$cls_date=new cls_date();
		
		if(!empty($start_date))
		{
			$start_time=$cls_date->date_to_stamp($start_date);
			$smarty->assign('start_date',  $start_date);
		}
		else{
			$start_time='';
		}
		
		if(!empty($end_date))
		{
			$end_time=$cls_date->date_to_stamp($end_date);
			$smarty->assign('end_date',  $end_date);
		}
		else{
			$end_time='';
			$smarty->assign('end_date',  date('Y-m-d',time()));
		}		
		
		if(!empty($goods_sn))
		{
			$sql="select goods_id from ".$GLOBALS['ecs']->table('goods')." where goods_sn='".$goods_sn."'";
			$goods_id=$GLOBALS['db']->getOne($sql);			
		}
		
		$stock_record=get_stock_record('',$stock_style,$goods_id,$warehouse_id,$warehousing_delivery_style_id,$start_time,$end_time);

		if($_REQUEST['act'] == 'print_W_D_record')
		{
			$smarty->assign('stock_record',  $stock_record);
			$smarty->display('erp_stock_record_print.htm');
		}
		elseif($_REQUEST['act'] == 'export_W_D_record')
		{
					require('./includes/lib/cls/cls_csv.php');	
					require('./includes/lib/cls/cls_file.php');	
					include('../includes/cls_json.php');
					
					$export=array();
	
					//csv首行
					$row['goods_sn']=$_LANG['erp_stock_record_goods_sn'];
					$row['goods_name']=$_LANG['erp_stock_record_goods_name'];
					$row['goods_attr']=$_LANG['erp_stock_record_goods_attr'];
					$row['warehouse_name']=$_LANG['erp_stock_record_warehouse_name'];
					$row['w_d_style']=$_LANG['erp_stock_record_w_d_style'];
					$row['w_d_sn']=$_LANG['erp_stock_record_w_d_sn'];
					$row['order_sn']=$_LANG['erp_stock_record_order_sn'];
					$row['w_d_qty']=$_LANG['erp_stock_record_w_d_qty'];
					$row['w_d_name']=$_LANG['erp_stock_record_w_d_name'];
					$row['record_date']=$_LANG['erp_stock_record_date'];
					
					$export[]=$row;
					
					if(!empty($stock_record))
					{
						foreach($stock_record as $record)
						{
								$row['goods_sn']=$record['goods_info']['goods_sn'];
								$row['goods_name']=$record['goods_info']['goods_name'];
								
								if(!empty($record['goods_attr']))
								{
										$attrs='';
										foreach($record['goods_attr']['attr_info'] as $attr)
										{
											$attrs=$attrs.' '.$attr['attr_value'];
										}
										$row['goods_attr']=$attrs;
								}
								else{
									$row['goods_attr']='$$';
								}

								$row['warehouse_name']=$record['warehouse_name'];
								$row['w_d_style']=$record['w_d_style'];
								$row['w_d_sn']=$record['w_d_sn'];
								$row['order_sn']=$record['order_sn'];
								$row['w_d_qty']=$record['qty'];
								$row['w_d_name']=$record['w_d_name'];
								$row['record_date']=$record['act_date'];	
								
								$export[]=$row;						
						}
					}
					
					
					$csv=new cls_csv();
					$file_stream=$csv->encode($export);
					
					$file_stream=iconv('utf-8','gb2312',$file_stream);
					
					$file=new cls_file();
					
					$path=ROOT_PATH.'data/stock_record/';
					$random_name=random_string('numeric', 8);
					$file_name=date('Y-m-d').'-'.$random_name.'.csv';
					
					while(file_exists($path.$file_name))//如果文件已存在那么重新生成名字
					{
						$random_name=random_string('numeric', 8);
						$file_name=date('Y-m-d').'-'.$random_name.'.csv';			
					}
					
					$file->write_file($path,$file_name,$file_stream,$op='rewrite');	//生成文件
					
					$json  = new JSON;
					
					$server_address=$_SERVER["SERVER_NAME"];
					$phpself=$_SERVER["PHP_SELF"];
					$path=str_replace(ADMIN_PATH.'/erp_warehouse_manage.php','',$phpself);
					$base_url=$server_address.$path;	
					$url='http://'.$base_url.'data/stock_record/'.$file_name;
				
					$result['error']=0;
					$result['url']=$url;
					
					die($json->encode($result));
		}
		
	}
	else{
		$href="index.php?act=main";
		$text=$_LANG['erp_retun_to_center'];
		$link[] = array('href' => $href, 'text' => $text);	

   	sys_msg($_LANG['erp_no_permit'], 0, $link);		
	}		
}

?>